package com.example.administrator.appli;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.appli.observ.Observered;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Observable;
import java.util.Observer;

public class AsynTaskActivity extends Activity implements Async.IAsync, Observer {

    private static final String TAG = "AsynTaskActivity";
    TextView tv_asynctask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_asyn_task);
        tv_asynctask = (TextView) findViewById(R.id.tv_asynctask);

        tv_asynctask.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Async async = new Async(tv_asynctask, AsynTaskActivity.this);
                async.execute("baidu.com", "1111111", "123");
            }
        });


        Observered observ = new Observered();
        Log.e(TAG, "onCreate: observ");
        observ.addObserver(this);
        try {
            observ.notifyChange(5);
            Log.e(TAG, "onCreate: try");
        } catch (InterruptedException e) {
            e.printStackTrace();
            Log.e(TAG, "InterruptedException: ");
        }
    }


    @Override
    public void pre(Object obj) {
        Log.e(TAG, "pre: " + obj);

    }

    @Override
    public void doing(Object obj) {
        Log.e(TAG, "doing: " + obj);

    }

    @Override
    public void finish(Object object) {
        Log.e(TAG, "finish: " + object);

    }

    String s = "";

    @Override
    public void update(Observable observable, final Object data) {

        s = s+data+"";

        tv_asynctask.setText("--==--" + s);

        Log.e(TAG, "update: " + data);
    }
}

class Async extends AsyncTask<String, Integer, String> {
    private static final String TAG = "Async";
    TextView tv;

    IAsync iAsync;

    public Async(TextView tv, IAsync iAsync) {
        this.tv = tv;
        this.iAsync = iAsync;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        tv.setText("onPostExecute");
        iAsync.pre("接口pre。。。");
        Log.e(TAG, "onPreExecute: ");
    }

    @Override
    protected String doInBackground(String... params) {
        Log.e(TAG, "doInBackground: " + params[0]);
        int sum = 0;
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(200);
                iAsync.doing(i);
                Log.e(TAG, "doInBackground: " + i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sum += i;
        }
        URI uri;
        try {
            uri = new URI("http://www.baidu.com");
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        return String.valueOf(sum);
    }


    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        Log.e(TAG, "onProgressUpdate: " + values[0]);
    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        tv.setText("onPostExecute" + s);
        Log.e(TAG, "onPostExecute: " + s);
        iAsync.finish("接口finish。。。");
    }

    /**
     * 接口实现回调
     */
    interface IAsync {
        void pre(Object obj);

        void doing(Object obj);

        void finish(Object object);
    }
}
